package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 上传音视频文件到腾讯云点播平台
     *
     * @param file
     * @return {mediaFileId:文件唯一标识,mediaUrl:文件播放地址}
     */
    @Operation(summary = "上传音视频文件到腾讯云点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    /**
     * TODO 需要登录才能访问
     * 保存声音
     *
     * @param trackInfoVo
     * @return
     */
    @GuiguLogin
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@Validated @RequestBody TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }

    /**
     * 详情页面查询专辑包含声音列表-动态展示付费标识
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiguLogin(required = false)
    @Operation(summary = "详情页面查询专辑包含声音列表-动态展示付费标识")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(@PathVariable Long albumId,
                                                            @PathVariable int page,
                                                            @PathVariable int limit) {
        //1.获取当前登录用户id
        Long userId = AuthContextHolder.getUserId();

        //2.构建分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);

        //3.调用业务层获取声音列表
        pageInfo = trackInfoService.getAlbumTrackPage(pageInfo, albumId, userId);
        return Result.ok(pageInfo);

    }

    /**
     * 获取声音的统计信息
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "获取声音的统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    /**
     * 根据待购声音作为起始标准查询当前用户未购买的声音列表
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @GuiguLogin
    @Operation(summary = "根据待购声音作为起始标准查询当前用户未购买的声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,
                                                         @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }


}

